﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbArticlesDal
	{
		public DbArticlesDal()
		{
		}
 		#region Insert
 		public static int Insert(DbArticles dbArticles)
		{	
			string sql = @"Insert Into DbArticles (
					ATitle,
					AClassId,
					AHtmlTitle,
					AKeywords,
					ADescription,
					ANote,
					AShortInstr,
					AStatus,
					AHits,
					AOrder,
					AAddTime,
					AAllClassIds,
					ALang,
					APicture,
					AFrom,
					AAuthor,
					AIsPhotoNews,
					AClassIds,
					CEndDate
				) values('{0}',{1},'{2}','{3}','{4}','{5}','{6}',{7},{8},{9},'{10}','{11}','{12}','{13}','{14}','{15}',{16},'{17}','{18}')";
			sql = string.Format(sql,
					SqlHelper.String(dbArticles.ATitle),
					dbArticles.AClassId,
					SqlHelper.String(dbArticles.AHtmlTitle),
					SqlHelper.String(dbArticles.AKeywords),
					SqlHelper.String(dbArticles.ADescription),
					dbArticles.ANote,
					SqlHelper.String(dbArticles.AShortInstr),
					dbArticles.AStatus,
					dbArticles.AHits,
					dbArticles.AOrder,
					dbArticles.AAddTime,
					SqlHelper.String(dbArticles.AAllClassIds),
					SqlHelper.String(dbArticles.ALang),
					SqlHelper.String(dbArticles.APicture),
					SqlHelper.String(dbArticles.AFrom),
					SqlHelper.String(dbArticles.AAuthor),
					dbArticles.AIsPhotoNews,
					SqlHelper.String(dbArticles.AClassIds),
					dbArticles.CEndDate
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbArticles dbArticles)
		{	
			string sql = @"Update DbArticles set 

					ATitle= '{0}',
					AClassId= {1},
					AHtmlTitle= '{2}',
					AKeywords= '{3}',
					ADescription= '{4}',
					ANote= '{5}',
					AShortInstr= '{6}',
					AStatus= {7},
					AHits= {8},
					AOrder= {9},
					AAddTime= '{10}',
					AAllClassIds= '{11}',
					ALang= '{12}',
					APicture= '{13}',
					AFrom= '{14}',
					AAuthor= '{15}',
					AIsPhotoNews= {16},
					AClassIds= '{17}',
					CEndDate= '{18}'
				where

					AId= {19}";
			sql = string.Format(sql,
					SqlHelper.String(dbArticles.ATitle),
					dbArticles.AClassId,
					SqlHelper.String(dbArticles.AHtmlTitle),
					SqlHelper.String(dbArticles.AKeywords),
					SqlHelper.String(dbArticles.ADescription),
					dbArticles.ANote,
					SqlHelper.String(dbArticles.AShortInstr),
					dbArticles.AStatus,
					dbArticles.AHits,
					dbArticles.AOrder,
					dbArticles.AAddTime,
					SqlHelper.String(dbArticles.AAllClassIds),
					SqlHelper.String(dbArticles.ALang),
					SqlHelper.String(dbArticles.APicture),
					SqlHelper.String(dbArticles.AFrom),
					SqlHelper.String(dbArticles.AAuthor),
					dbArticles.AIsPhotoNews,
					SqlHelper.String(dbArticles.AClassIds),
					dbArticles.CEndDate,
					dbArticles.AId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 aId)
		{
		
			string sql = @"Delete from DbArticles where 
					AId= {0}";
			sql = string.Format(sql,
					aId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbArticles a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbArticles Select(Int32 aId)
		{
			DbArticles dbArticles = null;
			string sql = @"select a.* from DbArticles a  where
					a.AId= {0}
";
			sql = string.Format(sql,
					aId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbArticles = new DbArticles(ds.Tables[0].Rows[0]);					
			return dbArticles;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbArticles a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbArticles a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbArticles a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
